<?php

/**
 * Project: Espolon
 * Licence: BSD License
 * Link: http://espolon.org/
 * Copyright: Nicolas de Bari Embriz Garcia Rojas <nbari at espolon dot org>
 * Author: Nicolas de Bari Embriz Garcia Rojas <nbari at espolon dot org>
 * Version: Beta
 */

if (!defined('SITE_ROOT')) exit('No direct script access allowed');

class EditConfig extends HTML_QuickForm {
  private $tpl;
  private $conf;

  function EditConfig($tpl,$conf) {
    $this->tpl = $tpl;
    $this->conf = $conf;

    foreach ($conf->getLanguages() as $lang) {
      $select_language[$lang] = $lang;
    }

    if($conf->getThemes()) {
			foreach ($conf->getThemes() as $theme) {
				$select_theme[$theme] = $theme;
			}
		} else {
			$select_theme = null;
		}

    $defaults = array('site_name' => $conf->getConfig('site_name'),
                      'site_slogan' => $conf->getConfig('site_slogan'),
                      'site_language' => $conf->getConfig('site_language'),
                      'site_theme' => $conf->getConfig('site_theme'),
                      'email_fromname' => $conf->getConfig('email_fromname'),
                      'email_from' => $conf->getConfig('email_from'),
											'allow_signup' => $conf->getConfig('allow_signup'),
                      'allow_force_login' => $conf->getConfig('allow_force_login'),
                      'avoid_logging_twice' => $conf->getConfig('avoid_logging_twice')
                      );

   $this->setDefaults($defaults);

    parent::HTML_QuickForm('config');
    $this->addElement('header', 'header', 'config');
    $this->addElement('text', 'site_name', _('site name'), array('size' => 20, 'maxlength' => 255));
    $this->addElement('text', 'site_slogan', _('site slogan'), array('size' => 50, 'maxlength' => 255));
    $this->addElement('select','site_language', _('site language'), $select_language);
		$this->addElement('select','site_theme', _('site theme'), $select_theme);
    $this->addElement('text', 'email_fromname', _('email fromname'), array('size' => 30, 'maxlength' => 255));
    $this->addElement('text', 'email_from', _('email from'), array('size' => 30, 'maxlength' => 255));
    $allow_force_login[] =& $this->createElement('radio', null, null, _('enable'), 1, array('class' => 'radio'));
    $allow_force_login[] =& $this->createElement('radio', null, null, _('disable'), 0, array('class' => 'radio'));
    $avoid_logging_twice[] =& $this->createElement('radio', null, null, _('enable'), 1, array('class' => 'radio'));
    $avoid_logging_twice[] =& $this->createElement('radio', null, null, _('disable'), 0, array('class' => 'radio'));
    $this->addGroup($allow_force_login, 'allow_force_login', _('allow force login'), '&nbsp;');
    $this->addGroup($avoid_logging_twice, 'avoid_logging_twice', _('avoid logging twice'), '&nbsp;');
		$allow_signup[] =& $this->createElement('radio', null, null, _('enable'), 1, array('class' => 'radio'));
    $allow_signup[] =& $this->createElement('radio', null, null, _('disable'), 0, array('class' => 'radio'));
		$this->addGroup($allow_signup, 'allow_signup', _('allow signup'), '&nbsp;');

    $this->addElement('submit', 'btn_Submit', _('submit'), array('class' => 'submit', 'onmouseover' => 'this.style.backgroundColor = \'#E0E7E7\'; this.style.borderColor = \'#728FA4\';', 'onmouseout' => 'this.style.backgroundColor = \'#eeeeee\'; this.style.borderColor = \'#999999\';'));
    $this->applyFilter('__ALL__', 'trim');
    $this->addRule('email_from', $tpl->get_config_vars('error_email'), 'email');
  }

  function displayForm() {
    $this->renderer = new HTML_QuickForm_Renderer_ArraySmarty($this->tpl);
    $this->renderer->setRequiredTemplate(
  		    '{if $error}
  		 	     <font color="red">{$label}</font>
  	 	     {else}
  	 		     {$label}
  		 	     {if $required}
  	            <font color="red" size="1">*</font>
  	   	     {/if}
  	       {/if}'
    );
    $this->renderer->setErrorTemplate(
          '{if $error}
  	  	    <font color="blue" size="1">{$error}</font><br />
  	       {/if}{$html}'
    );
    $this->setRequiredNote(
   		'<small class="red">*&nbsp;</small><small class="blue">'._('required fields').'</small>'
   	);
   	$this->accept($this->renderer);
   	$this->tpl->assign('form_data',$this->renderer->toArray());
   	$this->tpl->assign('msg',$this->conf->getMsg());
  }

  function process() {
    $this->conf->SaveConfig($this->getSubmitValues());
    $this->tpl->assign('result',true);
    $this->tpl->assign('msg',$this->conf->getMsg());
  }

} // end of class

?>